package TAIC.util;

import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map.Entry;


/**
 * This program cale the number of files in each class and cheak each sift file
 * @author Kent
 *
 */
public class CheckSiftIntegrity {
	static public void main ( String [] argu ) {
		if ( argu.length < 1 ) {
			System.out.println ( "CheckSiftIntegrity filename" );
			return ;
		}
		try {
			HashMap < String , Integer >  count = new HashMap< String, Integer> ();
			Scanner configScanner = new Scanner ( new File ( argu[ 0 ] ));
			PrintStream fout = new PrintStream ( new File ( argu [ 0 ] + ".bak")) ;
			System.out.print( "Checking" ) ; 
			while ( configScanner.hasNext()){
				System.out.print( ".") ;
				String p1 = configScanner.next() ;
				String p2 = configScanner.next() ;
				
				String fileName = configScanner.nextLine().trim() ;
				File siftFile = new File ( fileName + ".sift"); 
				if  ( ! siftFile.exists() ) continue ;
				Scanner fileScanner = new Scanner ( siftFile ) ;
				int lines = fileScanner.nextInt () ; 
				fileScanner.nextLine();
				int i ; 
				for ( i = 0 ; i < lines ; i ++ )
					if ( ! fileScanner.hasNextLine() ) continue ;
				
				if ( i >= lines ) fout.println ( p1 + " "  + p2 + " " + fileName ) ;
				String dirName = fileName.substring(0, fileName.indexOf('\\') ) ;
				if ( count.get( dirName ) == null ) count.put( dirName, 1) ;
				else count.put( dirName,  count.get(dirName) + 1 ) ; 
			}
			PrintStream totalOut = new PrintStream ( new File ( "imageInfo.txt" )) ;
			for ( Entry <String, Integer> e :count.entrySet()) totalOut.println (e.getKey () + "\t" + e.getValue() ) ; 
			totalOut.close () ;
			fout.close() ; 
			configScanner.close() ; 
			GetODP.copyFile( new File (argu[ 0 ] + ".bak"), new File ( argu[ 0 ]) ) ; 
		}catch ( Exception e ) {
			e.printStackTrace(); 
		}
	}
}
